/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.bioingenium.kbmed.features.io;

import co.edu.unal.bioingenium.kbmed.config.Configuration;
import co.edu.unal.bioingenium.kbmed.features.CollectionFeatures;
import co.edu.unal.bioingenium.kbmed.features.CompactFeatureVector;
import co.edu.unal.bioingenium.kbmed.text.representation.io.CorpusIO;
import co.edu.unal.bioingenium.kbmed.text.representation.vo.Corpus;
import co.edu.unal.bioingenium.kbmed.util.io.FileManager;
import gnu.trove.map.TIntObjectMap;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alejandro Riveros Cruz Riveros Cruz
 */
public class FeaturesIO {

    /**
     *
     * @param corpus 
     * @param collectionFeatures
     * @param name  
     */
    public static void storePlainCollectionFeatures(Corpus corpus, CollectionFeatures collectionFeatures, String name) {
        BufferedWriter bufferedWriter = null;
        try {
            String path = Configuration.EXPERIMENTS_DIR + File.separator + FileManager.getStandardName(name) + "PlainFeatures.txt";
            bufferedWriter = new BufferedWriter(new FileWriter(path));
            CompactFeatureVector featureVector;
            TIntObjectMap<CompactFeatureVector> featuresMap = collectionFeatures.getCollectionFeatures();
            List<String> documentList = corpus.getSortedDocumentList();
            for (int index : featuresMap.keys()) {
                bufferedWriter.write(documentList.get(index) + ":");
                featureVector = featuresMap.get(index);
                for (int featureIndex : featureVector.getSortedIndexKeys()) {
                    bufferedWriter.write("(" + featureIndex + "," + featureVector.getValue(featureIndex) + ")");
                }
                bufferedWriter.newLine();
            }
            bufferedWriter.flush();
            bufferedWriter.close();
        } catch (IOException ioex) {
            Logger.getLogger(CorpusIO.class.getName()).log(Level.SEVERE, null, ioex);
        } finally {
            try {
                bufferedWriter.close();
            } catch (IOException ex) {
                Logger.getLogger(CorpusIO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     *
     * @param collectionFeatures
     * @param name  
     */
    public static void storeCollectionFeatures(CollectionFeatures collectionFeatures, String name) {
        FileOutputStream fileOutputStream = null;
        try {
            File file = new File(Configuration.FEATURE_DIR + File.separator + name);
            if (!file.exists()) {
                file.createNewFile();
            }
            fileOutputStream = new FileOutputStream(file);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(collectionFeatures);
            objectOutputStream.close();
        } catch (IOException ioex) {
            Logger.getLogger(FeaturesIO.class.getName()).log(Level.SEVERE, null, ioex);
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException ex) {
                Logger.getLogger(FeaturesIO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     *
     * @param name
     * @return 
     */
    public static CollectionFeatures loadCollectionFeatures(String name) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(Configuration.FEATURE_DIR + File.separator + name);
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            CollectionFeatures collectionFeatures = (CollectionFeatures) objectInputStream.readObject();
            objectInputStream.close();
            return collectionFeatures;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(FeaturesIO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ioex) {
            Logger.getLogger(FeaturesIO.class.getName()).log(Level.SEVERE, null, ioex);
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException ex) {
                Logger.getLogger(FeaturesIO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }
}
